package controller;

import model.User;
import services.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserService();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("输入的用户名：" + username);
        System.out.println("输入的密码：" + password);
        String registerMsg = null;
        if (!"".equals(username) && !"".equals(password)) {
            int res = userService.doRegister(username, password);
            if (res==0){
                registerMsg = "用户名已注册！";
            }
        } else {
            registerMsg = "请输入需要用户名和密码！";
        }
        if (registerMsg == null) {
            response.sendRedirect("login.jsp");
        } else {
            request.setAttribute("register_msg", registerMsg);
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }
}
